{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.1 Importing a Data Set with the read_csv Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pokemon</th>\n",
       "      <th>Type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Bulbasaur</td>\n",
       "      <td>Grass / Poison</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Ivysaur</td>\n",
       "      <td>Grass / Poison</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Venusaur</td>\n",
       "      <td>Grass / Poison</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Charmander</td>\n",
       "      <td>Fire</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Charmeleon</td>\n",
       "      <td>Fire</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>804</th>\n",
       "      <td>Stakataka</td>\n",
       "      <td>Rock / Steel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>805</th>\n",
       "      <td>Blacephalon</td>\n",
       "      <td>Fire / Ghost</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>806</th>\n",
       "      <td>Zeraora</td>\n",
       "      <td>Electric</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>807</th>\n",
       "      <td>Meltan</td>\n",
       "      <td>Steel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>808</th>\n",
       "      <td>Melmetal</td>\n",
       "      <td>Steel</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>809 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         Pokemon            Type\n",
       "0      Bulbasaur  Grass / Poison\n",
       "1        Ivysaur  Grass / Poison\n",
       "2       Venusaur  Grass / Poison\n",
       "3     Charmander            Fire\n",
       "4     Charmeleon            Fire\n",
       "..           ...             ...\n",
       "804    Stakataka    Rock / Steel\n",
       "805  Blacephalon    Fire / Ghost\n",
       "806      Zeraora        Electric\n",
       "807       Meltan           Steel\n",
       "808     Melmetal           Steel\n",
       "\n",
       "[809 rows x 2 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The two lines below are equivalent\n",
    "pd.read_csv(filepath_or_buffer = \"pokemon.csv\")\n",
    "pd.read_csv(\"pokemon.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Type</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pokemon</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Bulbasaur</th>\n",
       "      <td>Grass / Poison</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ivysaur</th>\n",
       "      <td>Grass / Poison</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Venusaur</th>\n",
       "      <td>Grass / Poison</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Charmander</th>\n",
       "      <td>Fire</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Charmeleon</th>\n",
       "      <td>Fire</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Stakataka</th>\n",
       "      <td>Rock / Steel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Blacephalon</th>\n",
       "      <td>Fire / Ghost</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Zeraora</th>\n",
       "      <td>Electric</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Meltan</th>\n",
       "      <td>Steel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Melmetal</th>\n",
       "      <td>Steel</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>809 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                       Type\n",
       "Pokemon                    \n",
       "Bulbasaur    Grass / Poison\n",
       "Ivysaur      Grass / Poison\n",
       "Venusaur     Grass / Poison\n",
       "Charmander             Fire\n",
       "Charmeleon             Fire\n",
       "...                     ...\n",
       "Stakataka      Rock / Steel\n",
       "Blacephalon    Fire / Ghost\n",
       "Zeraora            Electric\n",
       "Meltan                Steel\n",
       "Melmetal              Steel\n",
       "\n",
       "[809 rows x 1 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\"pokemon.csv\", index_col = \"Pokemon\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pokemon\n",
       "Bulbasaur      Grass / Poison\n",
       "Ivysaur        Grass / Poison\n",
       "Venusaur       Grass / Poison\n",
       "Charmander               Fire\n",
       "Charmeleon               Fire\n",
       "                    ...      \n",
       "Stakataka        Rock / Steel\n",
       "Blacephalon      Fire / Ghost\n",
       "Zeraora              Electric\n",
       "Meltan                  Steel\n",
       "Melmetal                Steel\n",
       "Name: Type, Length: 809, dtype: object"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\"pokemon.csv\", index_col = \"Pokemon\", squeeze = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pokemon = pd.read_csv(\n",
    "    \"pokemon.csv\", index_col = \"Pokemon\", squeeze = True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2004-08-19</td>\n",
       "      <td>49.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2004-08-20</td>\n",
       "      <td>53.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2004-08-23</td>\n",
       "      <td>54.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2004-08-24</td>\n",
       "      <td>52.24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2004-08-25</td>\n",
       "      <td>52.80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Date  Close\n",
       "0  2004-08-19  49.98\n",
       "1  2004-08-20  53.95\n",
       "2  2004-08-23  54.50\n",
       "3  2004-08-24  52.24\n",
       "4  2004-08-25  52.80"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\"google_stocks.csv\").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2004-08-19</td>\n",
       "      <td>49.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2004-08-20</td>\n",
       "      <td>53.95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2004-08-23</td>\n",
       "      <td>54.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2004-08-24</td>\n",
       "      <td>52.24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2004-08-25</td>\n",
       "      <td>52.80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date  Close\n",
       "0 2004-08-19  49.98\n",
       "1 2004-08-20  53.95\n",
       "2 2004-08-23  54.50\n",
       "3 2004-08-24  52.24\n",
       "4 2004-08-25  52.80"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\"google_stocks.csv\", parse_dates = [\"Date\"]).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2004-08-19    49.98\n",
       "2004-08-20    53.95\n",
       "2004-08-23    54.50\n",
       "2004-08-24    52.24\n",
       "2004-08-25    52.80\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\n",
    "    \"google_stocks.csv\",\n",
    "    parse_dates = [\"Date\"],\n",
    "    index_col = \"Date\",\n",
    "    squeeze = True\n",
    ").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "google = pd.read_csv(\n",
    "    \"google_stocks.csv\",\n",
    "    parse_dates = [\"Date\"],\n",
    "    index_col = \"Date\",\n",
    "    squeeze = True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Battle</th>\n",
       "      <th>Start Date</th>\n",
       "      <th>State</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>227</th>\n",
       "      <td>Siege of Fort Henry</td>\n",
       "      <td>9/11/1782</td>\n",
       "      <td>Virginia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>228</th>\n",
       "      <td>Grand Assault on Gibraltar</td>\n",
       "      <td>9/13/1782</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>229</th>\n",
       "      <td>Action of 18 October 1782</td>\n",
       "      <td>10/18/1782</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>230</th>\n",
       "      <td>Action of 6 December 1782</td>\n",
       "      <td>12/6/1782</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>231</th>\n",
       "      <td>Action of 22 January 1783</td>\n",
       "      <td>1/22/1783</td>\n",
       "      <td>Virginia</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         Battle  Start Date     State\n",
       "227         Siege of Fort Henry   9/11/1782  Virginia\n",
       "228  Grand Assault on Gibraltar   9/13/1782       NaN\n",
       "229   Action of 18 October 1782  10/18/1782       NaN\n",
       "230   Action of 6 December 1782   12/6/1782       NaN\n",
       "231   Action of 22 January 1783   1/22/1783  Virginia"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\"revolutionary_war.csv\").tail() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Battle</th>\n",
       "      <th>State</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Start Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1782-09-11</th>\n",
       "      <td>Siege of Fort Henry</td>\n",
       "      <td>Virginia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1782-09-13</th>\n",
       "      <td>Grand Assault on Gibraltar</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1782-10-18</th>\n",
       "      <td>Action of 18 October 1782</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1782-12-06</th>\n",
       "      <td>Action of 6 December 1782</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1783-01-22</th>\n",
       "      <td>Action of 22 January 1783</td>\n",
       "      <td>Virginia</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                Battle     State\n",
       "Start Date                                      \n",
       "1782-09-11         Siege of Fort Henry  Virginia\n",
       "1782-09-13  Grand Assault on Gibraltar       NaN\n",
       "1782-10-18   Action of 18 October 1782       NaN\n",
       "1782-12-06   Action of 6 December 1782       NaN\n",
       "1783-01-22   Action of 22 January 1783  Virginia"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\n",
    "    \"revolutionary_war.csv\",\n",
    "    index_col = \"Start Date\",\n",
    "    parse_dates = [\"Start Date\"],\n",
    ").tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1782-09-11    Virginia\n",
       "1782-09-13         NaN\n",
       "1782-10-18         NaN\n",
       "1782-12-06         NaN\n",
       "1783-01-22    Virginia\n",
       "Name: State, dtype: object"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\n",
    "    \"revolutionary_war.csv\",\n",
    "    index_col = \"Start Date\",\n",
    "    parse_dates = [\"Start Date\"],\n",
    "    usecols = [\"State\", \"Start Date\"],\n",
    "    squeeze = True,\n",
    ").tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "battles = pd.read_csv(\n",
    "    \"revolutionary_war.csv\",\n",
    "    index_col = \"Start Date\",\n",
    "    parse_dates = [\"Start Date\"],\n",
    "    usecols = [\"State\", \"Start Date\"],\n",
    "    squeeze = True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.2 Sorting a Series"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.1 Sorting by Values with the sort_values Method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2004-09-03      49.82\n",
       "2004-09-01      49.94\n",
       "2004-08-19      49.98\n",
       "2004-09-02      50.57\n",
       "2004-09-07      50.60\n",
       "               ...   \n",
       "2019-04-23    1264.55\n",
       "2019-10-25    1265.13\n",
       "2018-07-26    1268.33\n",
       "2019-04-26    1272.18\n",
       "2019-04-29    1287.58\n",
       "Name: Close, Length: 3824, dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google.sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pokemon\n",
       "Illumise                Bug\n",
       "Silcoon                 Bug\n",
       "Pinsir                  Bug\n",
       "Burmy                   Bug\n",
       "Wurmple                 Bug\n",
       "                  ...      \n",
       "Tirtouga       Water / Rock\n",
       "Relicanth      Water / Rock\n",
       "Corsola        Water / Rock\n",
       "Carracosta     Water / Rock\n",
       "Empoleon      Water / Steel\n",
       "Name: Type, Length: 809, dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    Adam\n",
       "2     Ben\n",
       "1    adam\n",
       "dtype: object"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series(data = [\"Adam\", \"adam\", \"Ben\"]).sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2019-04-29    1287.58\n",
       "2019-04-26    1272.18\n",
       "2018-07-26    1268.33\n",
       "2019-10-25    1265.13\n",
       "2019-04-23    1264.55\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google.sort_values(ascending = False).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pokemon\n",
       "Empoleon      Water / Steel\n",
       "Corsola        Water / Rock\n",
       "Relicanth      Water / Rock\n",
       "Carracosta     Water / Rock\n",
       "Tirtouga       Water / Rock\n",
       "Name: Type, dtype: object"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.sort_values(ascending = False).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1781-09-06    Connecticut\n",
       "1779-07-05    Connecticut\n",
       "1777-04-27    Connecticut\n",
       "1777-09-03       Delaware\n",
       "1777-05-17        Florida\n",
       "                 ...     \n",
       "1782-08-08            NaN\n",
       "1782-08-25            NaN\n",
       "1782-09-13            NaN\n",
       "1782-10-18            NaN\n",
       "1782-12-06            NaN\n",
       "Name: State, Length: 232, dtype: object"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The two lines below are equivalent\n",
    "battles.sort_values()\n",
    "battles.sort_values(na_position = \"last\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1775-09-17         NaN\n",
       "1775-12-31         NaN\n",
       "1776-03-03         NaN\n",
       "1776-03-25         NaN\n",
       "1776-05-18         NaN\n",
       "                ...   \n",
       "1781-07-06    Virginia\n",
       "1781-07-01    Virginia\n",
       "1781-06-26    Virginia\n",
       "1781-04-25    Virginia\n",
       "1783-01-22    Virginia\n",
       "Name: State, Length: 232, dtype: object"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.sort_values(na_position = \"first\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1781-09-06    Connecticut\n",
       "1779-07-05    Connecticut\n",
       "1777-04-27    Connecticut\n",
       "1777-09-03       Delaware\n",
       "1777-05-17        Florida\n",
       "                 ...     \n",
       "1781-07-06       Virginia\n",
       "1781-07-01       Virginia\n",
       "1781-06-26       Virginia\n",
       "1781-04-25       Virginia\n",
       "1783-01-22       Virginia\n",
       "Name: State, Length: 162, dtype: object"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.dropna().sort_values()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.2 Sorting by Index with the sort_index Method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pokemon\n",
       "Abomasnow        Grass / Ice\n",
       "Abra                 Psychic\n",
       "Absol                   Dark\n",
       "Accelgor                 Bug\n",
       "Aegislash      Steel / Ghost\n",
       "                  ...       \n",
       "Zoroark                 Dark\n",
       "Zorua                   Dark\n",
       "Zubat        Poison / Flying\n",
       "Zweilous       Dark / Dragon\n",
       "Zygarde      Dragon / Ground\n",
       "Name: Type, Length: 809, dtype: object"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The two lines below are equivalent\n",
    "pokemon.sort_index()\n",
    "pokemon.sort_index(ascending = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1774-09-01    Massachusetts\n",
       "1774-12-14    New Hampshire\n",
       "1775-04-19    Massachusetts\n",
       "1775-04-19    Massachusetts\n",
       "1775-04-20         Virginia\n",
       "                  ...      \n",
       "1783-01-22         Virginia\n",
       "NaT              New Jersey\n",
       "NaT                Virginia\n",
       "NaT                     NaN\n",
       "NaT                     NaN\n",
       "Name: State, Length: 232, dtype: object"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "NaT              New Jersey\n",
       "NaT                Virginia\n",
       "NaT                     NaN\n",
       "NaT                     NaN\n",
       "1774-09-01    Massachusetts\n",
       "Name: State, dtype: object"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.sort_index(na_position = \"first\").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1783-01-22    Virginia\n",
       "1782-12-06         NaN\n",
       "1782-10-18         NaN\n",
       "1782-09-13         NaN\n",
       "1782-09-11    Virginia\n",
       "Name: State, dtype: object"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.sort_index(ascending = False).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2.3 Retrieving the Smallest and Largest Values with the nsmallest and nlargest Methods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2019-04-29    1287.58\n",
       "2019-04-26    1272.18\n",
       "2018-07-26    1268.33\n",
       "2019-10-25    1265.13\n",
       "2019-04-23    1264.55\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google.sort_values(ascending = False).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2019-04-29    1287.58\n",
       "2019-04-26    1272.18\n",
       "2018-07-26    1268.33\n",
       "2019-10-25    1265.13\n",
       "2019-04-23    1264.55\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The two lines below are equivalent\n",
    "google.nlargest(n = 5)\n",
    "google.nlargest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2004-09-03    49.82\n",
       "2004-09-01    49.94\n",
       "2004-08-19    49.98\n",
       "2004-09-02    50.57\n",
       "2004-09-07    50.60\n",
       "Name: Close, dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The two lines below are equivalent\n",
    "google.nsmallest(n = 5)\n",
    "google.nsmallest(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.3 Overwriting a Series with the inplace Parameter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1774-09-01    Massachusetts\n",
       "1774-12-14    New Hampshire\n",
       "1775-04-19    Massachusetts\n",
       "Name: State, dtype: object"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1781-09-06    Connecticut\n",
       "1779-07-05    Connecticut\n",
       "1777-04-27    Connecticut\n",
       "Name: State, dtype: object"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.sort_values().head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1774-09-01    Massachusetts\n",
       "1774-12-14    New Hampshire\n",
       "1775-04-19    Massachusetts\n",
       "Name: State, dtype: object"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1774-09-01    Massachusetts\n",
       "1774-12-14    New Hampshire\n",
       "1775-04-19    Massachusetts\n",
       "Name: State, dtype: object"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "battles.sort_values(inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1781-09-06    Connecticut\n",
       "1779-07-05    Connecticut\n",
       "1777-04-27    Connecticut\n",
       "Name: State, dtype: object"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.4 Counting Values with the value_counts Method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pokemon\n",
       "Bulbasaur     Grass / Poison\n",
       "Ivysaur       Grass / Poison\n",
       "Venusaur      Grass / Poison\n",
       "Charmander              Fire\n",
       "Charmeleon              Fire\n",
       "Name: Type, dtype: object"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Normal                65\n",
       "Water                 61\n",
       "Grass                 38\n",
       "Psychic               35\n",
       "Fire                  30\n",
       "                      ..\n",
       "Fighting / Flying      1\n",
       "Psychic / Fighting     1\n",
       "Ground / Steel         1\n",
       "Fighting / Ghost       1\n",
       "Fighting / Dark        1\n",
       "Name: Type, Length: 159, dtype: int64"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "159"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(pokemon.value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "159"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fighting / Dark       1\n",
       "Bug / Ground          1\n",
       "Fire / Rock           1\n",
       "Dragon / Electric     1\n",
       "Psychic / Steel       1\n",
       "                     ..\n",
       "Fire                 30\n",
       "Psychic              35\n",
       "Grass                38\n",
       "Water                61\n",
       "Normal               65\n",
       "Name: Type, Length: 159, dtype: int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.value_counts(ascending = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Normal     0.080346\n",
       "Water      0.075402\n",
       "Grass      0.046972\n",
       "Psychic    0.043263\n",
       "Fire       0.037083\n",
       "Name: Type, dtype: float64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.value_counts(normalize = True).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Normal     8.034611\n",
       "Water      7.540173\n",
       "Grass      4.697157\n",
       "Psychic    4.326329\n",
       "Fire       3.708282\n",
       "Name: Type, dtype: float64"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.value_counts(normalize = True).head() * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Normal                8.03\n",
       "Water                 7.54\n",
       "Grass                 4.70\n",
       "Psychic               4.33\n",
       "Fire                  3.71\n",
       "                      ... \n",
       "Fighting / Flying     0.12\n",
       "Psychic / Fighting    0.12\n",
       "Ground / Steel        0.12\n",
       "Fighting / Ghost      0.12\n",
       "Fighting / Dark       0.12\n",
       "Name: Type, Length: 159, dtype: float64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(pokemon.value_counts(normalize = True) * 100).round(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "194.27    3\n",
       "307.10    3\n",
       "290.41    3\n",
       "287.68    3\n",
       "237.04    3\n",
       "Name: Close, dtype: int64"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google.value_counts().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1287.58"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "49.82"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(200.0, 400.0]      1568\n",
       "(-0.001, 200.0]      595\n",
       "(400.0, 600.0]       575\n",
       "(1000.0, 1200.0]     406\n",
       "(600.0, 800.0]       380\n",
       "(800.0, 1000.0]      207\n",
       "(1200.0, 1400.0]      93\n",
       "Name: Close, dtype: int64"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "buckets = [0, 200, 400, 600, 800, 1000, 1200, 1400]\n",
    "google.value_counts(bins = buckets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-0.001, 200.0]      595\n",
       "(200.0, 400.0]      1568\n",
       "(400.0, 600.0]       575\n",
       "(600.0, 800.0]       380\n",
       "(800.0, 1000.0]      207\n",
       "(1000.0, 1200.0]     406\n",
       "(1200.0, 1400.0]      93\n",
       "Name: Close, dtype: int64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google.value_counts(bins = buckets).sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-0.001, 200.0]      595\n",
       "(200.0, 400.0]      1568\n",
       "(400.0, 600.0]       575\n",
       "(600.0, 800.0]       380\n",
       "(800.0, 1000.0]      207\n",
       "(1000.0, 1200.0]     406\n",
       "(1200.0, 1400.0]      93\n",
       "Name: Close, dtype: int64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google.value_counts(bins = buckets, sort = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(48.581, 256.113]      1204\n",
       "(256.113, 462.407]     1104\n",
       "(462.407, 668.7]        507\n",
       "(668.7, 874.993]        380\n",
       "(874.993, 1081.287]     292\n",
       "(1081.287, 1287.58]     337\n",
       "Name: Close, dtype: int64"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "google.value_counts(bins = 6, sort = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Start Date\n",
       "1781-09-06    Connecticut\n",
       "1779-07-05    Connecticut\n",
       "1777-04-27    Connecticut\n",
       "1777-09-03       Delaware\n",
       "1777-05-17        Florida\n",
       "Name: State, dtype: object"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "South Carolina    31\n",
       "New York          28\n",
       "New Jersey        24\n",
       "Virginia          21\n",
       "Massachusetts     11\n",
       "Name: State, dtype: int64"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.value_counts().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "NaN               70\n",
       "South Carolina    31\n",
       "New York          28\n",
       "New Jersey        24\n",
       "Virginia          21\n",
       "Name: State, dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.value_counts(dropna = False).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['1781-09-06', '1779-07-05', '1777-04-27', '1777-09-03',\n",
       "               '1777-05-17', '1779-09-10', '1779-09-07', '1780-03-02',\n",
       "               '1778-06-30', '1781-01-07',\n",
       "               ...\n",
       "               '1782-05-06', '1782-05-25', '1782-05-28', '1782-07-01',\n",
       "               '1782-07-06', '1782-08-08', '1782-08-25', '1782-09-13',\n",
       "               '1782-10-18', '1782-12-06'],\n",
       "              dtype='datetime64[ns]', name='Start Date', length=232, freq=None)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1782-01-11    2\n",
       "1778-09-07    2\n",
       "1780-05-25    2\n",
       "1782-03-16    2\n",
       "1781-09-13    2\n",
       "             ..\n",
       "1776-03-25    1\n",
       "1782-08-08    1\n",
       "1775-04-20    1\n",
       "1776-10-11    1\n",
       "1780-03-02    1\n",
       "Name: Start Date, Length: 217, dtype: int64"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "battles.index.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.5 Invoking a Function on Every Series Value with the apply Method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "funcs = [len, max, min]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3824\n",
      "1287.58\n",
      "49.82\n"
     ]
    }
   ],
   "source": [
    "funcs = [len, max, min]\n",
    "\n",
    "for current_func in funcs:\n",
    "    print(current_func(google))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "99"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(99.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "99"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(99.49)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(99.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Date\n",
       "2004-08-19      50\n",
       "2004-08-20      54\n",
       "2004-08-23      54\n",
       "2004-08-24      52\n",
       "2004-08-25      53\n",
       "              ... \n",
       "2019-10-21    1246\n",
       "2019-10-22    1243\n",
       "2019-10-23    1259\n",
       "2019-10-24    1261\n",
       "2019-10-25    1265\n",
       "Name: Close, Length: 3824, dtype: int64"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# The two lines below are equivalent\n",
    "google.apply(func = round)\n",
    "google.apply(round)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "def single_or_multi(pokemon_type):\n",
    "    if \"/\" in pokemon_type:\n",
    "        return \"Multi\"\n",
    "    \n",
    "    return \"Single\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pokemon\n",
       "Bulbasaur     Grass / Poison\n",
       "Ivysaur       Grass / Poison\n",
       "Venusaur      Grass / Poison\n",
       "Charmander              Fire\n",
       "Name: Type, dtype: object"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.head(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pokemon\n",
       "Bulbasaur       Multi\n",
       "Ivysaur         Multi\n",
       "Venusaur        Multi\n",
       "Charmander     Single\n",
       "Charmeleon     Single\n",
       "                ...  \n",
       "Stakataka       Multi\n",
       "Blacephalon     Multi\n",
       "Zeraora        Single\n",
       "Meltan         Single\n",
       "Melmetal       Single\n",
       "Name: Type, Length: 809, dtype: object"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.apply(single_or_multi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Multi     405\n",
       "Single    404\n",
       "Name: Type, dtype: int64"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pokemon.apply(single_or_multi).value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.6 Coding Challenge"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.6.1 Problems"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.6.2 Solutions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Saturday'"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import datetime as dt\n",
    "today = dt.datetime(2020, 12, 26)\n",
    "today.strftime(\"%A\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Battle</th>\n",
       "      <th>Start Date</th>\n",
       "      <th>State</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Powder Alarm</td>\n",
       "      <td>9/1/1774</td>\n",
       "      <td>Massachusetts</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Storming of Fort William and Mary</td>\n",
       "      <td>12/14/1774</td>\n",
       "      <td>New Hampshire</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Battles of Lexington and Concord</td>\n",
       "      <td>4/19/1775</td>\n",
       "      <td>Massachusetts</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Siege of Boston</td>\n",
       "      <td>4/19/1775</td>\n",
       "      <td>Massachusetts</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Gunpowder Incident</td>\n",
       "      <td>4/20/1775</td>\n",
       "      <td>Virginia</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              Battle  Start Date          State\n",
       "0                       Powder Alarm    9/1/1774  Massachusetts\n",
       "1  Storming of Fort William and Mary  12/14/1774  New Hampshire\n",
       "2   Battles of Lexington and Concord   4/19/1775  Massachusetts\n",
       "3                    Siege of Boston   4/19/1775  Massachusetts\n",
       "4                 Gunpowder Incident   4/20/1775       Virginia"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv(\"revolutionary_war.csv\").head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   1774-09-01\n",
       "1   1774-12-14\n",
       "2   1775-04-19\n",
       "3   1775-04-19\n",
       "4   1775-04-20\n",
       "Name: Start Date, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "days_of_war = pd.read_csv(\n",
    "    \"revolutionary_war.csv\",\n",
    "    usecols = [\"Start Date\"],\n",
    "    parse_dates = [\"Start Date\"],\n",
    "    squeeze = True,\n",
    ")\n",
    "\n",
    "days_of_war.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "def day_of_week(date):\n",
    "    return date.strftime(\"%A\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**NOTE**: I've commented out the code below so that the Notebook can run without raising an error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "# days_of_war.apply(day_of_week)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       Thursday\n",
       "1      Wednesday\n",
       "2      Wednesday\n",
       "3      Wednesday\n",
       "4       Thursday\n",
       "         ...    \n",
       "227    Wednesday\n",
       "228       Friday\n",
       "229       Friday\n",
       "230       Friday\n",
       "231    Wednesday\n",
       "Name: Start Date, Length: 228, dtype: object"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "days_of_war.dropna().apply(day_of_week)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Saturday     39\n",
       "Friday       39\n",
       "Wednesday    32\n",
       "Sunday       31\n",
       "Thursday     31\n",
       "Tuesday      29\n",
       "Monday       27\n",
       "Name: Start Date, dtype: int64"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "days_of_war.dropna().apply(day_of_week).value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.7 Summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
